package cn.hackcloud.javabase;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface ClotheFactory {
	public void clotheProduct();
}

class ClotheImpl implements ClotheFactory {
	@Override
	public void clotheProduct() {
		System.out.println("生产衣服哦。。。。。");
	}

}

class MyInvocationHandler implements InvocationHandler {
	Object obj;

	public Object bind(Object obj) {
		this.obj = obj;
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object returnVal = method.invoke(obj, args);
		return returnVal;
	}

}

public class TestProxy {
	public static void main(String[] args) {
		ClotheImpl clotheImpl = new ClotheImpl();
		MyInvocationHandler handler = new MyInvocationHandler();
		Object obj = handler.bind(clotheImpl);
		ClotheFactory clotheFatroy = (ClotheFactory) obj;
		clotheFatroy.clotheProduct();
	}
}